package com.qfedu.lc.user.controller;


import cn.hutool.core.convert.NumberWithFormat;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qfedu.lc.common.config.SystemConfig;
import com.qfedu.lc.common.param.UserVipAdd;
import com.qfedu.lc.common.util.JwtUtil;
import com.qfedu.lc.common.vo.R;
import com.qfedu.lc.user.entity.Vip;
import com.qfedu.lc.user.mapper.VipMapper;
import com.qfedu.lc.user.service.VipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 8.用户收费会员表 前端控制器
 * </p>
 *
 * @author KK
 * @since 2024-06-11
 */
@RestController
@RequestMapping("/user/vip")
public class VipController {

    @Autowired
    private VipService vipService;

    @Autowired
    private VipMapper vipMapper;

    @RequestMapping("/save")
    public R save(HttpServletRequest request, @RequestBody UserVipAdd userVipAdd) {
        //1.判断参数是否为空
        if (userVipAdd == null) {
            return R.fail("参数不能为空");
        }
        //配置userid
        String token = request.getHeader(SystemConfig.HEADER_TOKEN);
        Integer id = ((NumberWithFormat) JwtUtil.parseToken(token, "id")).intValue();
        userVipAdd.setUserId(id);

        //2.根据参数的类型不同 经不同的支付渠道
        switch (userVipAdd.getVipType()) {
            case 1://钱包支付
                return vipService.buyVipByWallet(userVipAdd);
            case 2://支付宝支付
                break;
            case 3://微信支付
                break;

        }
        return R.fail("开通失败");
    }

    @RequestMapping("/my")
    public R selectVip(HttpServletRequest request) {
        String token = request.getHeader(SystemConfig.HEADER_TOKEN);
        Integer id = ((NumberWithFormat) JwtUtil.parseToken(token, "id")).intValue();
        QueryWrapper<Vip> vipQueryWrapper = new QueryWrapper<>();
        vipQueryWrapper.eq("user_id", id);
        Vip vip = vipMapper.selectOne(vipQueryWrapper);
        if (vip == null) {
            return R.fail("不是vip");
        }
        return R.ok(vip);

    }
}

